﻿Shader "Dan/UI/UI_Radar01"
{
    Properties
    {
        _MainColor("Main Color",Color)=(1,1,1,1)
        _BlendColor("Blend Color",Color)=(1,1,1,1)
        _Emission("Emission", Float) = 1
        _Scanspeed("Scan speed", Float) = 0.25
        _ScanIntensity("Scan Intensity", Range( 0 , 1)) = 0.3
        _RadialSpeed("RadialSpeed", Float) = 0.5
        _RadialWidth("RadialWidth", Range( 0 , 1)) = 0.9
        
        _GridWidth("GridWidth", Range( 0 , 1)) = 0.05
        _GridY("GridY", Int) = 1
        _GridX("GridX", Int) = 1

    }
    
    SubShader
    {

        Tags { "RenderType"="Transparent" }
        LOD 100    
        Blend SrcAlpha OneMinusSrcAlpha
        ZWrite Off 
 
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            
            struct v2f
            {
                float4 vertex : SV_POSITION;    
                float2 uv : TEXCOORD0;
                
            };

             float4 _MainColor;
             float4 _BlendColor;
             float _Emission;
             float _Scanspeed;
             float _ScanIntensity;
             float _GridWidth;
             float _GridY;
             float _GridX;
             float _RadialSpeed;
             float _RadialWidth;
            
            v2f vert ( appdata v )
            {
                
                v2f o;
                UNITY_INITIALIZE_OUTPUT(v2f ,o);
                o.vertex = UnityObjectToClipPos(v.vertex);
                
                o.uv = v.uv;        
                return o;
            }
            
            


            fixed4 frag (v2f i ) : SV_Target
            {
                
                
                float2 mainUV = i.uv;
                
                //极坐标
                float2 polarUV = i.uv;
                polarUV-=0.5;
                float polar = atan2(polarUV.x  ,  polarUV.y  ) / 6.28318548202515;
                
                //扫描线
                float scanLine = frac(  polar - _Time.y * _Scanspeed  );
                scanLine = saturate(( scanLine  -0.8 ) * 5.0 );
                
                //圆形遮罩
                float circle = length( mainUV - 0.5 )*2;
                float sharpMask =step( circle , 0.95 );
                float blurMask = saturate(  1.0 - circle );
                
                
                // 辐射线
                float radialLine = frac(circle - _Time.y * _RadialSpeed);
                radialLine = saturate (( radialLine -_RadialWidth ) *6*_RadialWidth );
                
                //环形网格
                float p1 =step( frac( ( polar *32*_GridY ) ) , _GridWidth );
                float p2 =step( frac( ( circle*8*_GridX  ) ) , _GridWidth );
                float ring = p2+p1;
                
                
                //闪烁干扰
                float flicker = saturate( ( frac(  _Time.y * 60.0) + 0.66 ) );
                float noiseLine =sin(floor(circle*256)-_Time.y*60)*0.5+0.5;
                //noiseLine*=0.1;
                
                //float finalGrid = ring*scanLine +scanLine*radialLine ;
                
                float radar = scanLine*_ScanIntensity + ring*blurMask +radialLine + blurMask*0.5;
                
                float3 emissive =lerp(_MainColor,_BlendColor,blurMask)*_Emission;
                

                float4 finalColor = float4(emissive,radar*flicker*sharpMask); 
                return finalColor;
            }
            ENDCG
        }
    }
    
    
    
}
